<!--
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-06-04 14:00:13
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-14 14:21:47
 * @FilePath: /consult-patient-vue3-ts/src/views/Room/components/RoomMessage.vue
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
-->
<script setup lang="ts">
import dayjs from 'dayjs'
import { IllnessTime } from '@/enums'
import { flagOptions, timeOptions } from '@/services/constants'
import type { Message } from '@/types/room';
import { defineProps } from 'vue'
import { MsgType } from '@/enums'
import { ImagePreview } from 'vant'
import type { Image } from '@/types/consult'
import { useUserStore } from '@/stores'
import { getPrescriptionPic } from '@/services/consult'
import EvaluateCard from './EvaluateCard.vue'

const store = useUserStore()

defineProps<{ list: Message[] }>()
const getIllnessTimeText = (time: IllnessTime) => timeOptions.find((item) => item.value === time)?.label

const getConsultFlagText = (flag: 0 | 1) => flagOptions.find((item) => item.value === flag)?.label


const previewImg = (pictures?: Image[]) => {
  if (pictures && pictures.length) new ImagePreview(pictures.map((item) => item.url))
}
const formatTime = (time: string) => dayjs(time).format('HH:mm')

const showPrescription = async (id?: string) => {
  if (id) {
    const res = await getPrescriptionPic(id)
    new ImagePreview([res.data.url]) // 加了new不报错
  }
}
</script>

<template v-for="{ msgType, msg, createTime, from } in list" :key="msg.id">
  <!-- 病情描述 -->
  <div class="msg msg-illness" v-if="msgType === 21">
    <div class="patient van-hairline--bottom" v-if="msg.consultRecord">
      <p>
        {{ msg.consultRecord.patientInfo.name }}
        {{ msg.consultRecord.patientInfo.genderValue }}
        {{ msg.consultRecord.patientInfo.age }}岁
      </p>
      <p>
        {{ getIllnessTimeText(msg.consultRecord.illnessTime) }} |
        {{ getConsultFlagText(msg.consultRecord.consultFlag) }}
      </p>
    </div>
    <van-row>
      <van-col span="6">病情描述</van-col>
      <van-col span="18">{{ msg.consultRecord?.illnessDesc }}</van-col>
      <van-col span="6">图片</van-col>
      <van-col span="18" @click="previewImg(msg.consultRecord?.pictures)"> 点击查看 </van-col>
    </van-row>
  </div>
  <!-- 温馨提示 -->
  <div class="msg msg-tip" v-if="msgType === MsgType.NotifyTip">
    <div class="content">
      <span class="green">温馨提示：</span>
      <span>{{ msg.content }}</span>
    </div>
  </div>
  <!-- 通用通知 -->
  <div class="msg msg-tip" v-if="msgType === 31">
    <div class="content">
      <span>{{ msg.content }}</span>
    </div>
  </div>
  <!-- 我发的消息 -->
  <div class="msg msg-to" v-if="msgType === MsgType.MsgText && store.user?.id === from">
    <div class="content">
      <div class="time">{{ formatTime(createTime) }}</div>
      <div class="pao">{{ msg.content }}</div>
    </div>
    <van-image :src="store.user?.avatar" />
  </div>
  <!-- 医生发的消息 -->
  <div class="msg msg-from" v-if="msgType === MsgType.MsgText && store.user?.id !== from">
    <van-image :src="avatar" />
    <div class="content">
      <div class="time">{{ formatTime(createTime) }}</div>
      <div class="pao">{{ msg.content }}</div>
    </div>
  </div>
  <!-- 处方 -->
  <div class="msg msg-recipe" v-if="msgType === MsgType.CardPre">
    <div class="content" v-if="msg.prescription">
      <div class="head van-hairline--bottom">
        <div class="head-tit">
          <h3>电子处方</h3>
          <p @click="showPrescription(msg.prescription?.id)">
            原始处方 <van-icon name="arrow"></van-icon>
          </p>
        </div>
        <p>
          {{ msg.prescription.name }}
          {{ msg.prescription.genderValue }}
          {{ msg.prescription.age }}岁
          {{ msg.prescription.diagnosis }}
        </p>
        <p>开方时间：{{ msg.prescription.createTime }}</p>
      </div>
      <div class="body">
        <div class="body-item" v-for="med in msg.prescription.medicines" :key="med.id">
          <div class="durg">
            <p>{{ med.name }} {{ med.specs }}</p>
            <p>{{ med.usageDosag }}</p>
          </div>
          <div class="num">x{{ med.quantity }}</div>
        </div>
      </div>
      <div class="foot">
        <span @click="buy(msg.prescription)">购买药品</span>
      </div>
    </div>
  </div>
  <!-- 评价 -->
  <div class="msg msg-comment" v-if="msgType === MsgType.CardEva || msgType === MsgType.CardEvaForm">
    <evaluate-card :evaluateDoc="msg.evaluateDoc" />
  </div>
</template>

<style lang="scss" scoped>
@import '@/style/room.scss';
</style>